﻿using AutoMapper;
using B.S.Procurement.Domain;
using B.S.Procurement.Error;
using B.S.Procurement.Read.Api.Application.Command.PurchaseDetailsCommand;
using MediatR;
using SqlSugar;

namespace B.S.Procurement.Read.Api.Application.CommandHander.PurchaseDetailsCommandHander
{
    public class ShowPurchaseDetailsCommandHander : IRequestHandler<ShowPurchaseDetailsCommand, ApiResult<List<PurchaseDetails>>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public ShowPurchaseDetailsCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<ApiResult<List<PurchaseDetails>>> Handle(ShowPurchaseDetailsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<PurchaseDetails>> result = new ApiResult<List<PurchaseDetails>>();

            var list = await db.Queryable<PurchaseDetails>()
                                .Where(x => x.Del == false && x.DetailsId == request.DetailsId)
                                .ToListAsync();

            result.Data = list;

            result.Code = ApiEnum.success;
            result.Msg = "查询成功";

            return await Task.FromResult(result);
        }
    }
}
